package com.jintoufs.weixin.bank;

import com.alibaba.fastjson.JSON;
import com.jintoufs.domain.address.Address;
import com.jintoufs.domain.bank.BankCard;
import com.jintoufs.domain.bank.BankInfo;
import com.jintoufs.domain.constants.WxConstant;
import com.jintoufs.domain.customer.Customer;
import com.jintoufs.service.bank.BankService;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.util.weixin.HttpUtil;
import com.jintoufs.weixin.SupportAction;
import io.swagger.annotations.Api;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "银行卡相关接口", description = "银行卡相关接口")
@RestController
@RequestMapping("api/bank")
public class BankAction extends SupportAction {
    @Resource
    private BankService bankService;


    @GetMapping("oper")
    public AjaxMsg oper(String bankCardStr, String token, String type){
            BankCard bankCard= JSON.parseObject(bankCardStr,BankCard.class);
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        }else {

            switch (type){
                case "add":
                    Map<String,Object> param = new HashMap<>();
                    param.put("userId",customer.getId());
                    param.put("isDefault",true);
                    List<BankCard> bankCardList = bankService.selectByParam(param);
                    if (bankCardList.size()==0){
                        bankCard.setIsDefault(true);
                    }else {
                        if (bankCard.getIsDefault()){
                            bankCardList.get(0).setIsDefault(false);
                            bankService.save(bankCardList.get(0),null);
                        }
                    }
                    bankCard.setUserId(customer.getId());
                    bankService.save(bankCard,getUUID());
                    break;
                case "edit":
                    Map<String,Object> param2 = new HashMap<>();
                    param2.put("userId",customer.getId());
                    param2.put("isDefault",true);
                    List<BankCard> bankCardList2 = bankService.selectByParam(param2);
                    if (bankCardList2.size()==0){
                        bankCard.setIsDefault(true);
                    }else {
                        if (bankCard.getIsDefault()){
                            bankCardList2.get(0).setIsDefault(false);
                            bankService.save(bankCardList2.get(0),null);
                        }
                    }
                    bankCard.setUserId(customer.getId());
                    bankService.save(bankCard,null);
                    break;
                case "del":
                    bankService.delete(bankCard.getId());
                    break;
                default:
                    ajaxMsg.setCode(AjaxMsg.FAILURE);
                    ajaxMsg.setMessage("未知操作。");
                    break;
            }
        }
        return ajaxMsg;
    }


    @GetMapping("serach")
    public AjaxMsg serach(String token){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        }else {
            Map<String,Object> param = new HashMap<>();
            param.put("userId",customer.getId());
            try {
                List<BankCard> list=bankService.selectByParam(param);
                for(BankCard bankCard:list){
                    String bankAccount=bankCard.getAccountNumber();
                    int length=bankAccount.length();
                    String code="";
                    for(int i=1 ;i<=length-4;i++){
                        code+="*";
                        if(i%4==0){
                            code+=" ";
                        }
                    }
                    code+=" "+bankAccount.substring(length-4,length);
                    bankCard.setAccountNumber(code);
                }
                ajaxMsg.setDatas(list);
            }catch (Exception e){
                e.printStackTrace();
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("传入参数错误");
            }
        }
        return ajaxMsg;
    }

    @GetMapping("list")
    public AjaxMsg getBankList(String token){
        AjaxMsg ajaxMsg = new AjaxMsg();
        Customer customer = getCustomerByToken(token);
        if (ValidUtil.isEmpty(customer)) {
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("Token验证失败");
        }else {
            Map<String,Object> param = new HashMap<>();
            param.put("userId",customer.getId());
            try {
                List<BankCard> list=bankService.selectByParam(param);
                ajaxMsg.setDatas(list);
            }catch (Exception e){
                e.printStackTrace();
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("传入参数错误");
            }
        }
        return ajaxMsg;
    }

    @GetMapping("getBankInfo")
    public AjaxMsg getBankInfo(String cardNo){
        return bankService.getBankInfo(cardNo);
    }

}
